home *** CD-ROM | disk | FTP | other *** search
/ Delphi 2.0 - Programmer's Utilities Power Pack / Delphi 2.0 Programmer's Utilities Power Pack.iso / a_to_d / delftips / ti2791.asc < prev    next >
Encoding:
Text File  |  1996-09-15  |  4.3 KB  |  183 lines

  1.  
  2.  
  3.  
  4.  
  5.  
  6.  
  7.   PRODUCT  :  Delphi                                 NUMBER  :  2791
  8.   VERSION  :  All
  9.        OS  :  Windows
  10.      DATE  :  August 17, 1995                          PAGE  :  1/3
  11.  
  12.     TITLE  :  How to iterate through the fields of a table
  13.  
  14.  
  15.  
  16.  
  17. Getting a list of the fields in a table at run-time can be as simple as a
  18. call to the GetFieldNames method of the TTable, TQuery, or TStoredProc
  19. component. The DetFieldNames method returns a list of the fields that
  20. comprise the structure of the data set in the form of a TStrings list,
  21. which may be inserted into such visual components as a TListBox through
  22. its Items property:
  23.  
  24.   ListBox1.Clear;
  25.   Table1.GetFieldNames(ListBox1.Items);
  26.   
  27. Of course, the TStrings list returned by the GetFieldNames method need not
  28. be used with a visual component. It could just as well serve as an array
  29. of field names stored entirely in memory, that can be used as a list or
  30. array.
  31.   
  32. But it is also possible to retrieve much more information about the
  33. fields in a table than just the names. Other descriptive attributes incl-
  34. ude field types and sizes. Retrieving tyhese values is slightly more
  35. involved than the use of the GetFieldNames. Basically, this process
  36. involves iterating through the FieldDefs property of the TTable, TQuery,
  37. or TStoredProc component. The FieldDefs property is essentially an
  38. array of records, one record for each field in the structure. Each field
  39. record contains information about the field, including its name, type,
  40. and size. It is a relatively straightforward process to iterate through
  41. this array of field descriptions, extracting information about individual
  42. fields.
  43.  
  44. There are a number of reasons why a program might need to query the
  45. structure of a table used in the application. One reason is a prelude to
  46. creating TField components at run-time that represent the fields in the
  47. table. The information gleaned from the structure of the table form the
  48. basis of the TField components to be created.
  49.  
  50. The example below demonstrates how to iterate through the fields available
  51. in a TTable or TQuery. The example extracts information about the available
  52. fields and displays the information in a TListBox, but the same methodology
  53. can be used to provide information necessary for the dynamic building of
  54. TField descendants. The example uses a TTable as the data set, but a TQuery
  55.  
  56.  
  57.  
  58.  
  59.  
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.  
  68.   PRODUCT  :  Delphi                                 NUMBER  :  2791
  69.   VERSION  :  All
  70.        OS  :  Windows
  71.      DATE  :  August 17, 1995                          PAGE  :  2/3
  72.  
  73.     TITLE  :  How to iterate through the fields of a table
  74.  
  75.  
  76.  
  77.  
  78. can be used in the same manner as both TTable and TQuery components incorp-
  79. orate the Field-Defs property the same way.
  80.  
  81. procedure TForm1.Button1Click(Sender: TObject);
  82. var
  83.   i: Integer;
  84.   F: TFieldDef;
  85.   D: String;
  86. begin
  87.   Table1.Active := True;
  88.   ListBox1.Items.Clear;
  89.   with Table1 do begin
  90.     for i := 0 to FieldDefs.Count - 1 do begin
  91.       F := FieldDefs.Items[i];
  92.       case F.DataType of
  93.         ftUnknown: D := 'Unknown';
  94.         ftString: D := 'String';
  95.         ftSmallint: D := 'SmallInt';
  96.         ftInteger: D := 'Integer';
  97.         ftWord: D := 'Word';
  98.         ftBoolean: D := 'Boolean';
  99.         ftFloat: D := 'Float';
  100.         ftCurrency: D := 'Currency';
  101.         ftBCD: D := 'BCD';
  102.         ftDate: D := 'Date';
  103.         ftTime: D := 'Time';
  104.         ftDateTime: D := 'DateTime';
  105.         ftBytes: D := 'Bytes';
  106.         ftVarBytes: D := '';
  107.         ftBlob: D := 'BLOB';
  108.         ftMemo: D := 'Memo';
  109.         ftGraphic: D := 'Graphic';
  110.       else
  111.         D := '';
  112.       end;
  113.       ListBox1.Items.Add(F.Name + ', ' + D);
  114.     end;
  115.   end;
  116.  
  117.  
  118.  
  119.  
  120.  
  121.  
  122.  
  123.  
  124.  
  125.  
  126.  
  127.  
  128.  
  129.   PRODUCT  :  Delphi                                 NUMBER  :  2791
  130.   VERSION  :  All
  131.        OS  :  Windows
  132.      DATE  :  August 17, 1995                          PAGE  :  3/3
  133.  
  134.     TITLE  :  How to iterate through the fields of a table
  135.  
  136.  
  137.  
  138.  
  139.   Table1.Active := False;
  140. end;
  141.  
  142.  
  143.  
  144.  
  145.  
  146.  
  147.  
  148.  
  149.  
  150.  
  151.  
  152.  
  153.  
  154.  
  155.  
  156.  
  157.  
  158.  
  159.  
  160.  
  161.  
  162.  
  163.  
  164.  
  165.  
  166.  
  167.  
  168.  
  169.  
  170.  
  171.  
  172.  
  173.  
  174.  
  175.  
  176.  
  177.  
  178.  
  179. DISCLAIMER: You have the right to use this technical information
  180. subject to the terms of the No-Nonsense License Statement that
  181. you received with the Borland product to which this information
  182. pertains.
  183.